package lfr.ustc.ftp;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail implements Runnable {
	public static final SendMail send = new SendMail();

	private static String smtp_host = "smtp.gmail.com";
	private static String from_account = "zdyxlk@gmail.com";
	private static String from_passWord = "jxnktjlfr";
	private static String from_port = "25";
	private String show_name = "furong";

	public static String email_address;
	public static String email_subject;
	public static String email_content;
	public static String[] sendTo;

	static Session session;
	static Transport transport = null;

	private SendMail() {

	}

	public static SendMail getInsatance() {
		return send;
	}

	public static void setProg(String account, String pwd, String host,
			String port) {
		from_account = account;
		from_passWord = pwd;
		smtp_host = host;
		from_port = port;
		initProps();
	}

	public static void setMessage(String[] to, String subject, String content,
			String show_name) {
		sendTo = to;
		email_subject = subject;
		email_content = content;
	}

	public void start(boolean isDamon) {
		Thread t = new Thread(this);
		t.setDaemon(isDamon);
		t.start();
	}

	public void run() {
		executeMailSend(sendTo, email_subject, email_content);
	}


	public void executeMailSend(String[] to, String sendSubject, String sendText) {

		try {
			// �ռ���
			InternetAddress[] sendTo = new InternetAddress[to.length];
			for (int i = 0; i < to.length; i++) {
				System.out.println("���͵�:" + to[i]);
				sendTo[i] = new InternetAddress(to[i]);
			}

			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(from_account));

			message.setRecipients(Message.RecipientType.TO, sendTo);
			message.setFrom(new InternetAddress(show_name + "<" + from_account
					+ ">"));
			message.setSubject(sendSubject);
			message.setContent(sendText, "text/html;charset=utf-8");
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("failure! ");

		}
	}

	private static void initProps() {
		Properties props = System.getProperties();

		props.put("mail.smtp.host", smtp_host); // ����SMTP���
		props.put("mail.smtp.auth", "true"); // �Ƿ񵽷������û����������֤

		props.setProperty("mail.smtp.port", from_port);
		props.setProperty("mail.smtp.socketFactory.port", from_port);
		if (from_port.equals("465")) {
			props.setProperty("mail.smtp.socketFactory.class",
					"javax.net.ssl.SSLSocketFactory");

		}

		session = Session.getDefaultInstance(props, null);
		session.setDebug(false);
		try {
			transport = session.getTransport("smtp");
			transport.connect(smtp_host, from_account, from_passWord);
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	public boolean connect() {
		boolean isConnect = false;
		isConnect = transport.isConnected();
		return isConnect;
	}

}
